package com.example.community.service;

import com.example.community.entity.Message;
import com.example.community.mapper.MessageMapper;
import com.example.community.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    // 根据用户id查询私信列表
    public List<Message> findConversations(int userId, int offset, int limit){
        return messageMapper.selectConversations(userId, offset, limit);
    }
    // 查询总的私信数量
    public int findConversationCount(int userId){
        return messageMapper.selectConversationCount(userId);
    }
    // 根据私信id查询私信内容
    public List<Message> findLetters(String conversationId, int offset, int limit){
        return messageMapper.selectLetters(conversationId, offset, limit);
    }
    // 私信总记录数
    public int findLetterCount(String conversationId){
        return messageMapper.selectLetterCount(conversationId);
    }
    // 私信未读数量
    public int findLetterUnreadCount(int userId, String conversationId){

        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }
    // 添加私信
    public int addMessage(Message message){
        // 过滤敏感词
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }
    // 读取私信 -- 改变私信状态
    public int readMessage(List<Integer> ids){
        return messageMapper.updateStatus(ids, 1);
    }

    // 查询关于某主题最新的系统通知
    public Message findLatestNotice(int userId, String topic){
        return messageMapper.selectLatestNotice(userId, topic);
    }

    // 查询系统通知的总记录数
    public int findNoticeCount(int userId, String topic){
        return messageMapper.selectNoticeCount(userId, topic);
    }

    // 查询未读的系统通知数量
    public int findNoticeUnreadCount(int userId, String topic){
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    // 查询某个主题的通知列表
    public List<Message> findNotices(int userId, String topic, int offset, int limit){
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }



}

